package com.xsf.pay.common.inteceptor;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.xsf.pay.common.utils.RequestUtil;
import com.xsf.pay.common.utils.Tools;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/**
 * 日志打印
 * @author 18961
 */
@Slf4j
@Component
public class LogInterceptor implements HandlerInterceptor {

    /**
     * 请求前处理器
     * @param request 请求体
     * @param response 返回体
     * @param handler 处理器
     * @return 返回是够成功
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (handler instanceof HandlerMethod) {
            try {
                StringBuilder sb = new StringBuilder(1000);
                sb.append("\n-----------------------").append(DateUtil.now())
                        .append("-------------------------------------\n");
                HandlerMethod h = (HandlerMethod) handler;
                String bodyString = RequestUtil.getBodyString(request);
                sb.append("REQ_URL   :").append(request.getRequestURL()).append("\n");
                sb.append("IP        :").append(Tools.getIpAddr(request)).append("\n");
                sb.append("Controller: ").append(h.getMethod().getDeclaringClass().getName()).append("\n");
                sb.append("Method    : ").append(h.getMethod().getName()).append("\n");
                if (Tools.isTooLager(bodyString)) {
                    log.info("LogCostInterceptor：preHandle  Params  当前查询参数过大，不打印");
                } else {
                    sb.append("Params    : ").append(JSON.parseObject(bodyString)).append("\n");
                }
                sb.append("URI       : ").append(request.getRequestURI()).append("\n");
                log.info(sb.toString());
                if (Tools.isTooLager(bodyString)) {
                    log.info("LogCostInterceptor：preHandle当前查询参数过大，不打印");
                } else {
                    log.info("获取后" + bodyString);
                }
            }catch (Exception e){
                log.error("接口日志输出异常："+e);
            }
        }
        return true;
    }

    /**
     * 请求后处理器
     * @param request 请求体
     * @param response 返回体
     * @param handler 处理器
     * @param modelAndView 试图处理器
     * @throws Exception 错误
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 所有请求结束
     * @param request 请求体
     * @param response 返回体
     * @param handler 处理器
     * @param ex 错误
     * @throws Exception 错误
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
